/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class AboutBox extends JDialog {
	private JLabel label = new JLabel();
	private StringBuilder stringBuilder = new StringBuilder();
	private InputStream inputStream;
	private JButton credits = new JButton("Credits");
	private TextFrame creditsBox = new TextFrame("documents/credits.html", "Credits", "text/html");
	private TextFrame licenseBox = new TextFrame("documents/license.html", "Stones License", "text/html");
	private JButton license = new JButton("License");
	private JButton close = new JButton("Close");
	private GridBagConstraints constraints = new GridBagConstraints();
	
	public AboutBox() {
		int ch;
		try {
			this.inputStream = GamePlayer.getInputStream("documents/about.html");
			while ((ch = this.inputStream.read()) != -1) {
				this.stringBuilder.append((char)ch);
			}
		} catch (Exception e) {
			this.stringBuilder = new StringBuilder();
			this.stringBuilder.append("Failed to load file");
		}
		
		this.label.setText(this.stringBuilder.toString());
		
		this.close.setMnemonic(KeyEvent.VK_C);
		this.license.setMnemonic(KeyEvent.VK_L);
		this.credits.setMnemonic(KeyEvent.VK_R);
		
		this.getContentPane().setLayout(new GridBagLayout());
		this.constraints.gridwidth = 4;
		this.constraints.insets = new Insets(5, 5, 5, 5);
		this.constraints.weightx = 1;
		this.constraints.weighty = 1;
		this.constraints.gridx = 0;
		this.constraints.gridy = 0;
		this.constraints.anchor = GridBagConstraints.PAGE_START;
		this.getContentPane().add(this.label, this.constraints);
		
		this.constraints.weightx = 0;
		this.constraints.weighty = 0;
		this.constraints.gridy = 1;
		this.constraints.gridwidth = 1;
		this.constraints.anchor = GridBagConstraints.LINE_START;
		this.getContentPane().add(this.credits, this.constraints);
		
		this.constraints.gridx = 1;
		this.getContentPane().add(this.license, this.constraints);
		
		this.constraints.gridx = 3;
		this.constraints.anchor = GridBagConstraints.LINE_END;
		this.getContentPane().add(this.close, this.constraints);
		
		this.license.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				licenseBox.setVisible(true);
			}
			
		});
		
		this.close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
			
		});
		
		this.credits.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				creditsBox.setVisible(true);
			}
			
		});
		
		this.pack();
		this.setResizable(false);
		this.setTitle("About Stones");
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setResizable(false);
	}
}